pub fn main() {
    let nc = NumCat{num:12, name:"123"};
    let nc2 = NumCat2{num:12, name:"123"};

     // dyn 可以不写 默认会加 ，但是属于过期写法 会在下一版中禁用
    let arr2:Vec<Box<dyn RevString>> = vec![Box::new(nc), 
            Box::new(nc2)];

    for vi in arr2 {
        // 可以根据实际类型 调用不同方法，实现多态
        println!("vi={}", vi.to_rev());
    }
}

fn run() {
    let arr=[1,9,5];
    println!("max1={}",max(&arr));
    let arr=['A','P','E'];
    println!("max2={}",max(&arr));
    let arr=["A","Z","R"];
    println!("max3={}",max2(&arr));
    let mut s = "init";
    {
        let s2="123".to_string();
        s= max_str(s, &s2);
        println!("s={}",s);
    }
    let n1=1;
    let n2=3;
    let mut nc = NumCat::new();
    nc.num = 12;
    println!("nc={}", nc.to_string());
    let res = nc.get_num(&3);
    println!("res={}",res);
    let nc2 = NumCat::new_2("jackn");
    println!("nc2={}", nc2.to_string());
    println!("nc2={}", nc2.to_rev());
}
struct NumCat<'a>{
    num: i32,
    name: &'a str,
}
struct NumCat2<'a>{
    num: i32,
    name: &'a str,
}
impl <'a> RevString for NumCat2<'a>{
    fn to_rev(&self)->String{
        format!("NumCat2{{num={}, name={}}}", self.num,self.name)
    }
    // fn get_one<'b,T>(&self, list: &'b Vec<T>)-> &'b T{
    //     &list[1]
    // }
}
impl <'a> NumCat<'a>{
    // 如果返回值和第二个值相关时，也必须声明声明周期
    fn get_num<'b>(&'b self, y: &'b i32)-> &'b i32{
        &self.num
    }
    fn new()-> NumCat<'a>{
        // n 的声明周期会随着 NumCat 变长？
        let n=32;
        NumCat { num: n, name: ("ddd") }
    }
    fn new_2(name: &'a str)-> NumCat<'a>{
        // n 的声明周期会随着 NumCat 变长？
        let n=32;
        NumCat { num: n, name }
    }
}
trait RevString {
    fn to_rev(&self)-> String;
    // fn get_one<'a,T>(&self, list: &'a Vec<T>)-> &'a T;
}
impl <'a> ToString for NumCat<'a>{
    fn to_string(&self) -> String {
        format!("NumCat{{num={}, name={}}}", self.num, self.name)
    }
}
impl <'a> RevString for NumCat<'a>{
    fn to_rev(&self)->String{
        format!("NumCat{{name={}, num={}}}", self.name, self.num)
    }
    // fn get_one<'b,T>(&self, list: &'b Vec<T>)-> &'b T{
    //     &list[0]
    // }
}
// 返回引用
fn max_str<'a>(x:&'a str, y:&'a str)-> &'a str {
    if x.len() > y.len(){
        x
    }else{
        y
    }
}
// 返回有copy 属性的
fn max<T>(n: &[T])-> T
where T: std::cmp::PartialOrd+ Copy{
    let mut m = n[0];
    for &e in n {
        if e > m{
            m = e;
        }
    }
    m
}
// 返回克隆对象
fn max2<T>(n: &[T])-> T
where T: std::cmp::PartialOrd + std::clone::Clone{
    let mut m = n[0].clone();
    for e in n {
        if *e > m{
            m = e.clone();
        }
    }
    m
}